package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.util.StringUtil;
import cn.stylefeng.guns.core.util.TencentCloudUtils;
import cn.stylefeng.guns.modular.system.entity.SysSchool;
import cn.stylefeng.guns.modular.system.entity.SysStudent;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.factory.UserFactory;
import cn.stylefeng.guns.modular.system.mapper.SysStudentMapper;
import cn.stylefeng.guns.modular.system.model.StudentDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 学生用户关联表 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-06
 */
@Service
public class SysStudentService extends ServiceImpl<SysStudentMapper,SysStudent> {

    final private String PASSPORTPHOTO = "user/";
    @Autowired
    private UserService userService;

    public IPage<StudentDto> selectUsers(String name, String status, Long userId) {
       Long createUser = ShiroKit.isAdmin() ? null : ShiroKit.getUserNotNull().getId();
       return this.baseMapper.studentList (LayuiPageFactory.defaultPage(),name,status,createUser,userId);
    }

    public SysStudent getStudent (Long userId){
        SysStudent student = this.getOne(new QueryWrapper<SysStudent>()
                .eq("user_id", userId));
        return ToolUtil.isEmpty(student)?new SysStudent():student;
    }

    public void studentSave(StudentDto studentDto) {
        // 判断账号是否重复
        User theUser = userService.getByAccount(studentDto.getAccount());
        if (theUser != null && (ToolUtil.isEmpty(studentDto.getUserId()) || !theUser.getUserId().equals(studentDto.getUserId()))) {
            throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
        }
        // 完善账号信息
        String salt = "";
        String password = "";
        if(ToolUtil.isNotEmpty(studentDto.getPassword())){
            salt = ShiroKit.getRandomSalt(5);
            password = ShiroKit.md5(studentDto.getPassword(), salt);
        }
        if(ToolUtil.isEmpty(studentDto.getUserId())){
            studentDto.setStatus(ManagerStatus.OK.getCode());
            studentDto.setExamine(1);
        }else {
            studentDto.setStatus(ManagerStatus.FREEZED.getCode());
            studentDto.setExamine(0);
        }
        User user = UserFactory.createUser(studentDto, password, salt);
        user.setPhone(user.getAccount());
        user.setRoleId("1291579909321388034");
        userService.saveOrUpdate(user);

        SysStudent student = getStudent (user.getUserId());
        BeanUtils.copyProperties(studentDto, student);
        student.setUserId(user.getUserId());
        student.setCreateTime(LocalDateTime.now());
        student.setSchoolClassId(user.getDeptId());
        if(ToolUtil.isNotEmpty(student.getPassportPhoto())){
            String cover = PASSPORTPHOTO + StringUtil.getFileName(student.getPassportPhoto());
            TencentCloudUtils.copyObject(student.getPassportPhoto(), cover);
            student.setPassportPhoto(cover);
        }
        this.saveOrUpdate(student);
    }

    public Map<String,Object> studentInfo (String code){
        return this.baseMapper.studentInfo(code);
    }
}
